//index.js
//获取应用实例
const app = getApp()
const util = require('../../utils/util.js');
const api = require('../../utils/api.js');
Page({
	data: {
		newMan:false,
		phone:"",
		code:"",
		smsTrue:false,
		countdown:60,
		redBagList:[],
		smsName:"获取验证码",
	},
	onLoad: function(options) {
		if(options.isNew){
			this.setData({
		       newMan: true
		    })
		}else{
			this.setData({
		       newMan: false
		    })
		}
	},
	
	inputPhone:function(e){
		this.setData({
	      phone: e.detail.value
	    })
	},
	inputCode:function(e){
		this.setData({
	      code: e.detail.value
	    })
	},
	setTime:function(e){
		let _this = this;
		
		if (_this.data.countdown == 0) { 
	        _this.setData({
		       smsTrue: false,
		       countdown:60,
		       smsName:"获取验证码"
		    });
	   } else { 
			let time = _this.data.countdown;
			time--;
	    		_this.setData({
		       smsTrue: true,
		       smsName:time,
		       countdown:time
		    });
    		} 
		
	},
	sendSms:function(e){
		if(this.data.phone == ""){
			wx.showModal({
				 title: '提示',
				 content: "请输入手机号",
				 showCancel:false,
				 success: function(res) {
				  
				 }
			})
			return false;
		}else{
			if(!(/^1[34578]\d{9}$/.test(this.data.phone))){ 
		        wx.showModal({
					 title: '提示',
					 content: "手机号格式有误",
					 showCancel:false,
					 success: function(res) {
					  
					 }
				})  
		        return false; 
		    } 
		}
		
		let _this = this;
		api.sendSms({
			data: {
				phone:this.data.phone,
			},
			success: function(res) {
				if(res.data.code == 0) {
					
				}
			},
		});
	    let out = setInterval(function() { 
	    		if(_this.data.countdown == 0){
		    		clearInterval(out);
		    }
		    _this.setTime();
		    
		},1000); 
	},
	loginByPhone:function(e){
		let _this = this;
		api.loginByPhone({
			data: {
				userId:app.globalData.userId,
				phone:this.data.phone,
				//shareId:0,
				veriCode:this.data.code,
			},
			success: function(res) {
				console.log(res)
				if(res.data.code == 0) {
					_this.setData({
				       newMan: false,
				       redBagList:res.data.redBag
				    })
					
				}
			},
		});
	},
	tokcList:function(){
		wx.switchTab({
			url: '../kclist/kclist'
		})
	}
	
})